#include <stdio.h>
#include <stdlib.h>
#include "Common.h"

#define MAXSIZE 1000
typedef int ElemType;

//顺序列表栈
typedef struct{
    ElemType data[MAXSIZE];
    int top;
} SqStack;

//顺序列表共享栈
typedef struct{
    ElemType data[MAXSIZE];
    int top1;
    int top2;
}SqDoubleStack;

//链表栈，栈顶就是头指针，不需要头结点
typedef struct StackNode{
    ElemType data;
    struct StackNode *next;
}StackNode, *LinkStackPtr;
typedef struct LinkStack{
    LinkStackPtr top;
    int count;
} LinkStack;

Status linkStackPush(LinkStack *stack, ElemType e);

Status linkStackPop(LinkStack *stack, ElemType *e);

Status initDoubleStack(SqDoubleStack *stack);

Status initStack(SqStack *stack);

Status push(SqStack *stack, ElemType e);

Status pop(SqStack *stack, ElemType *e);

int main(){
    SqStack stack;
    stack.top = -1;
    initStack(&stack);
    printf("stack top=%d\n", stack.top);
    push(&stack,0);
    printf("stack top=%d\n", stack.top);

    SqDoubleStack doubleStack;
    initDoubleStack(&doubleStack);
    printf("top1=%d, top2=%d\n", doubleStack.top1, doubleStack.top2);
    LinkStack linkStack;
    linkStack.top = NULL;
    linkStack.count = 0;
    linkStackPush(&linkStack, 10);
    linkStackPush(&linkStack, 20);
    printf("linkStack top->data=%d, count=%d\n", linkStack.top->data, linkStack.count);
    ElemType er;
    linkStackPop(&linkStack, &er);
    printf("linkStack top->data=%d, count=%d\n", linkStack.top->data, linkStack.count);
    return 0;
}

Status linkStackPop(LinkStack *stack, ElemType *e){
    *e = stack->top->data;
    LinkStackPtr temp = stack->top->next;
    free(stack->top);
    stack->top = temp;
    stack->count--;
    return OK;
}

Status linkStackPush(LinkStack *stack, ElemType e){
    LinkStackPtr node = (LinkStackPtr)malloc(sizeof(StackNode));
    node->data = e;
    node->next = stack->top;
    stack->top = node;
    stack->count++;
    return OK;
}

Status initDoubleStack(SqDoubleStack *stack){
    stack->top1 = -1;
    stack->top2 = MAXSIZE-1;
    return OK;
}

Status initStack(SqStack *stack){
    stack->top = -1;
    return OK;
}

Status push(SqStack *stack, ElemType e){
    if(stack->top > MAXSIZE-1){
        return ERROR;//栈满
    }
    stack->top++;
    stack->data[stack->top] = e;
    return OK;
}

Status pop(SqStack *stack, ElemType *e){
    *e = stack->data[stack->top];
    stack->top--;
    return OK;
}